package com.zf.shuashua.ui.popup;

import android.app.Activity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.lxj.xpopup.core.CenterPopupView;
import com.zf.shuashua.R;
import com.zf.shuashua.api.Store;
import com.zf.shuashua.bean.VideoInfo;
import com.zf.shuashua.manager.AdAutoTouchManager;

/**
 * 自动播放弹框
 */
public class AutoPlayPopup extends CenterPopupView {

    private TextView btnTitle;
    private FrameLayout advert_container;
    private ImageView imgClose;
    private OnPopupListener mListener;
    private View view;

    public AutoPlayPopup(@NonNull Activity context) {
        super(context);
    }

    public void addAdvertView(View view) {
        this.view = view;
        advert_container.removeAllViews();
        advert_container.addView(view);
        btnTitle.setVisibility(VISIBLE);
        imgClose.setVisibility(VISIBLE);
    }

    public void loadAdvertFail() {
        btnTitle.setVisibility(VISIBLE);
        imgClose.setVisibility(VISIBLE);
        advert_container.setVisibility(GONE);
    }

    public void setPopupListener(OnPopupListener listener) {
        this.mListener = listener;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.popup_auto_play_new;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        setupView();
    }

    private void setupView() {
        btnTitle = findViewById(R.id.btTitle);
        advert_container = findViewById(R.id.advert_container);
        imgClose = findViewById(R.id.img_close);
        btnTitle.setOnClickListener(v -> {
            if (!AdAutoTouchManager.getInstance().randomAutoClickAd(view)) {
                dismiss();
                if (mListener != null) {
                    mListener.onNext();
                }
            }
        });


//        if (Store.getInstance().getTP_AD()) {
//            findViewById(R.id.flCancel).setOnClickListener(v -> {
//                dismiss();
//                if (mListener != null) {
//                    mListener.onNext();
//                }
//            });
//
//        } else {
//            btnTitle.setOnClickListener(v -> {
//                dismiss();
//                if (mListener != null) {
//                    mListener.onNext();
//                }
//            });
//        }


        imgClose.setOnClickListener(v -> {
            if (mListener != null) {
                mListener.onClose();
            }
        });
    }

}

